#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
    Module Documentation 
    here
"""

# Created by  : Zhang Chengdong
# Create Date : 2025/1/16 17:30
# Version = v0.1.0

__author__ = "Zhang Chengdong"
__copyright__ = "Copyright 2025. Large scale model"
__credits__ = ['Zhang Chengdong']

__liscence__ = "MIT"
__version__ = "1.0.1"
__maintainer__ = "Zhang Chengdong"
__status__ = "Production"

import logging

import pandas as pd
from scipy.stats import zscore

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


class AbnormalDataFilter:
    """
    异常数据过滤
    """

    def filter_outliners_by_boxplot(self, data: pd.DataFrame, columns: list,
                                    iqr_multiplier: float = 1.5) -> pd.DataFrame:
        """
        基于箱线图和 IQR 方法过滤异常值
        :param data:
        :param columns:
        :param iqr_multiplier:
        """
        if isinstance(columns, str):
            columns = [columns]

        # 初始化过滤后的数据和异常值
        filtered_data = data.copy()
        outliers = pd.DataFrame()
        # 对每一列进行异常值过滤
        for column in columns:
            Q1 = filtered_data[column].quantile(0.25)
            Q3 = filtered_data[column].quantile(0.75)
            IQR = Q3 - Q1

            lower_bound = Q1 - iqr_multiplier * IQR  # 下边界
            upper_bound = Q3 + iqr_multiplier * IQR  # 上边界

            column_outliers = filtered_data[
                (filtered_data[column] < lower_bound) | (filtered_data[column] > upper_bound)]
            filtered_data = filtered_data[
                (filtered_data[column] >= lower_bound) & (filtered_data[column] <= upper_bound)]

            logging.info("根据{}列的数据过滤掉：{}条数据".format(column, column_outliers.shape[0]))

        return filtered_data
